package io.gitee.mingbaobaba.apijson.querycondition.query.conditions;

import org.apache.commons.lang3.StringUtils;

/**
 * <p>条件片段</p>
 */
class ConditionSegment implements DslSegment {

    @Override
    public void apply(AbstractBaseWrapper<?, ?, ?> wrapper) {
        if (wrapper.conditionList.isEmpty()) {
            return;
        }
        if (wrapper.conditionList.stream().anyMatch(condition ->
                condition.getKeyword().equals(EnumKeyword.OR)
                        || condition.getKeyword().equals(EnumKeyword.AND)
                        || condition.getKeyword().equals(EnumKeyword.NOT))) {
            String keys = buildCombineKeys(wrapper);
            if (StringUtils.isNotBlank(keys)) {
                wrapper.apiJsonParams.put(EnumApiJson.COMBINE.getCode(), keys);
            }
        }
        wrapper.conditionList.forEach(condition -> {
            String column = getConditionColumn(condition);
            if (StringUtils.isNotBlank(column)) {
                wrapper.apiJsonParams.put(column, condition.getVal());
            }
        });
    }

    private String buildCombineKeys(AbstractBaseWrapper<?, ?, ?> wrapper) {
        StringBuilder keysBuilder = new StringBuilder();
        wrapper.conditionList.forEach(condition -> {
            if (condition.getKeyword().equals(EnumKeyword.OR)) {
                keysBuilder.append(EnumKeyword.OR.getApiJsonKeyword());
            } else if (condition.getKeyword().equals(EnumKeyword.AND)) {
                keysBuilder.append(EnumKeyword.AND.getApiJsonKeyword());
            } else if (condition.getKeyword().equals(EnumKeyword.NOT)) {
                keysBuilder.append(EnumKeyword.NOT.getApiJsonKeyword());
            } else {
                String column = getConditionColumn(condition);
                if (StringUtils.isNotBlank(column)) {
                    keysBuilder.append(column).append(",");
                }
            }
        });
        String keys = keysBuilder.toString();
        if (StringUtils.isBlank(keys)) {
            return "";
        }
        String formatted = keys.endsWith(",") ? keys.substring(0, keys.length() - 1) : keys;
        return formatted.split(",").length >= 2 ? formatted : "";
    }

    private String getConditionColumn(Condition condition) {
        switch (condition.getKeyword()) {
            case NULL:
            case NOT_NULL:
            case IN:
                return condition.getColumn() + EnumKeyword.IN.getApiJsonKeyword();
            case NOT_IN:
                return condition.getColumn() + EnumKeyword.NOT_IN.getApiJsonKeyword();
            case LIKE:
                return condition.getColumn() + EnumKeyword.LIKE.getApiJsonKeyword();
            case NOT_LIKE:
                return condition.getColumn() + EnumKeyword.NOT_LIKE.getApiJsonKeyword();
            case EQ:
                return condition.getColumn() + EnumKeyword.EQ.getApiJsonKeyword();
            case NE:
                return condition.getColumn() + EnumKeyword.NE.getApiJsonKeyword();
            case GT:
                return condition.getColumn() + EnumKeyword.GT.getApiJsonKeyword();
            case GE:
                return condition.getColumn() + EnumKeyword.GE.getApiJsonKeyword();
            case LT:
                return condition.getColumn() + EnumKeyword.LT.getApiJsonKeyword();
            case LE:
                return condition.getColumn() + EnumKeyword.LE.getApiJsonKeyword();
            case REG_EXP:
                return condition.getColumn() + EnumKeyword.REG_EXP.getApiJsonKeyword();
            case BETWEEN_AND:
                return condition.getColumn() + EnumKeyword.BETWEEN_AND.getApiJsonKeyword();
            default:
                return "";
        }
    }
}

